package com.useeinfo.oa.modules.logger.dao;

/**
 * Created by Cherry on 2018/10/8.
 */

import com.useeinfo.framework.extend.dao.CrudDao;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.QueryUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.logger.entity.ActiveLog;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("activeLogDao")
public class ActiveLogDao implements CrudDao<ActiveLog> {
    private final static Logger logger = LoggerFactory.getLogger(ActiveLogDao.class);

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public Map<String, Object> getSearchCondition(Map<String, String> queryHash) {
        Map<String, Object> conditionHash = new HashMap<>();
        if (queryHash == null || queryHash.size() == 0) {
            return conditionHash;
        }

        for (String queryKey : queryHash.keySet()) {
            String queryValue = queryHash.get(queryKey);

            switch (queryKey) {
                case "activeName":
                    if (StringUtils.isNotBlank(queryValue)) {
                        conditionHash.put("activeName like ?{paramIndex} ", "%" + queryValue + "%");
                    }
                    continue;

                case "keyNotes":
                    if(StringUtils.isNotEmpty(queryValue)){
                        StringBuilder sb = new StringBuilder();
                        sb.append(" ( ")
                                .append(" activeType like '%").append(queryValue).append("%' ")
                                .append(" or activeName like '%").append(queryValue).append("%' ")
                                .append(" or activeRemark like '%").append(queryValue).append("%' ")
                                .append(" or ip like '%").append(queryValue).append("%' ")
                                .append(" or name like ?{paramIndex} ")
                                .append(" ) ");
                        conditionHash.put(sb.toString(), "'%"+queryValue+"%'");
                    }
                    continue;

                case "startDate":
                    Date startDate = StringConverters.ToDateTime(queryValue);
                    if (startDate != null) {
                        conditionHash.put("createTime >= ?{paramIndex} ", startDate);
                    }
                    continue;

                case "endDate":
                    Date endDate = StringConverters.ToDateTime(queryValue);
                    if (endDate != null) {
                        conditionHash.put("createTime <= ?{paramIndex} ", endDate);
                    }
                    continue;
            }

            conditionHash.put("id < ?{paramIndex}", 0L);
            break;
        }

        return conditionHash;
    }

    @Override
    public Long totalRecord(Map<String, String> queryHash) {

        Map<String, Object> conditions = getSearchCondition(queryHash);
        TypedQuery<Long> typedQuery = QueryUtils.getTypedQueryByCondition("select count(a) from ActiveLog a ", conditions, entityManager, Long.class);
        return typedQuery.getSingleResult();
    }

    @Override
    public List<ActiveLog> findList(QueryParam queryParam) {

        String sqlInfo = queryParam.joinJPQL("select a from ActiveLog a ", "order by a.id desc ");

        Map<String, Object> conditions = getSearchCondition(queryParam.getSqlMap());
        TypedQuery<ActiveLog> typedQuery = QueryUtils.getTypedQueryByCondition(sqlInfo, conditions, entityManager, ActiveLog.class);

        return queryParam.findPageList(typedQuery);
    }

    @Override
    public ActiveLog findModel(Long id) {
        return entityManager.find(ActiveLog.class, id);
    }

    @Override
    public Integer add(ActiveLog model) {
        model.setCreateDate(new Date());
        entityManager.persist(model);
        logger.info("ActiveLogDaoImpl添加ActiveLogInfo成功！");
        return 1;
    }

    @Override
    public Integer update(ActiveLog model) {
        ActiveLog existActiveLog = entityManager.find(ActiveLog.class, model.getId());
        existActiveLog.setActiveRemark(model.getActiveRemark());
        existActiveLog.setIp(model.getIp());
        existActiveLog.setName(model.getName());
        existActiveLog.setCreateDate(new Date());
        return 1;
    }

    @Override
    public Integer delete(Long id) {
        ActiveLog existActiveLog = entityManager.find(ActiveLog.class, id);
        entityManager.remove(existActiveLog);
        return 1;
    }

    @Override
    public Integer deleteLogic(ActiveLog model) {
        return null;
    }


}
